import re
import requests
from bs4 import BeautifulSoup

i=1
hds={ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"}
for start_num in range(0,250,25):
    url=f"https://movie.douban.com/top250?start={start_num}"
    resp=requests.get(url,headers=hds)
    # print(resp.cookies)
    # dct=requests.utils.dict_from_cookiejar(resp.cookies)
    # print(dct)
    # cke=requests.utils.cookiejar_from_dict(dct)
    # print(cke)
    html=resp.text
    soup=BeautifulSoup(html,"html.parser")
    all_info = soup.findAll("span",attrs={"class":["title","inq"]})#如何同时查询多个，包括电影名称，评分，网址等
    # for title in all_titles:
    #     title_string=title.string
    #     if '/' not in title_string:
    #         print(i,end=":")
    #         i+=1
    #         print(title.string,end="\t")
    #     else:
    #         print(title.string)
    for info in all_info:
        if "title" in info["class"]:
        # Use regular expression to match movie names without slashes
            movie_match = re.match("(./.+)", info.text)
            if movie_match:
                print(f"{movie_match.group(1)}")
            else:
                print(f"{i}:{info.text}")
                i+=1
        elif "inq" in info["class"]:
            print(f"--{info.text}")
       
            